<template>
  <a-form :rules="commentRules" :model="commentFormConfig.form" @finish="submit" :label-col="{ span: 6 }"
    :wrapper-col="{ offset: 1 }" label-align="right" ref="commentFormRef" :disabled="hasPerm('ax:comment:edit')">
    <FormCard title="编辑/新增" :back="back" subtitle="系统评论">
      <template #extra>
        <a-button htmlType="submit" type="primary" :loading="commentTable.loading" block>
          保存
        </a-button>
      </template>
      <a-row :gutter="8">
        <a-col :span="16">
          <a-card>
            <a-form-item label="作者" name="author">
              <a-input allow-clear placeholder="请输入作者" v-model:value="commentFormConfig.form.author">
              </a-input>
            </a-form-item>
            <a-form-item label="内容" name="content">
              <a-textarea allow-clear placeholder="请输入内容" v-model:value="commentFormConfig.form.content"></a-textarea>
            </a-form-item>
            <a-form-item label="头像" name="avatar">
              <a-input allow-clear placeholder="请输入头像" v-model:value="commentFormConfig.form.avatar">
              </a-input>
            </a-form-item>
            <a-form-item label="喜欢" name="love">
              <a-input allow-clear placeholder="请输入喜欢" v-model:value="commentFormConfig.form.love">
              </a-input>
            </a-form-item>
            <a-form-item label="评论标记" name="flag">
              <a-input allow-clear placeholder="请输入评论标记" v-model:value="commentFormConfig.form.flag">
              </a-input>
            </a-form-item>
          </a-card>
        </a-col>
        <AxExtra></AxExtra>
      </a-row>
    </FormCard>
  </a-form>
</template>

<script setup lang="ts">
import {
} from '../data/options';
import { hasPerm } from '@/utils/common/perm';
import type { Response } from '@/api/config/types';
import {
  createComment,
  updateComment,
} from '@/api/modules/ax/comment/comment';
import { notify, } from '@/utils/table/table';
import { commentList } from '../data/curd';
import {
  commentFormConfig,
  commentRules,
  commentFormRef
} from '../data/form';
import { commentTable } from '../data/table';
import AxExtra from './AxExtra.vue';

const submit = async () => {
  commentTable.value.loading = true;
  let result: undefined | Response<any> = undefined
  if (commentFormConfig.value.form.id) {
    const { data } = await updateComment(commentFormConfig.value.form)
    result = data
  } else {
    const { data } = await createComment(commentFormConfig.value.form)
    result = data
  }
  notify(result)
  commentTable.value.loading = false;
  await commentList();
  back();

};

const back = () => {
  commentFormConfig.value.display = false;
  commentFormConfig.value.reset!();
};

</script>

<style lang="scss" scoped></style>
